-
Notifications
You must be signed in to change notification settings - Fork 7
Use two decimals for delayed scale factor #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use two decimals for delayed scale factor #114
Conversation
This is cleaner in code and it also makes it possible to provide a configurable percentage
This rebalances the bits to be more useful for anticipated values. A uint16 proves 65536 possible possibilities, but using basis points means that the values range from 0-6 with 4 decimals. As a percentage, this can represent up to 655 with two decimals.
Co-authored-by: Leo <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! Just a couple of comments
src/libs/LibPercentage.sol
Outdated
/// @param value The number to scale | ||
/// @param percentage The percentage expressed in basis points | ||
/// @return _ The scaled value | ||
function scaleBy(uint256 value, uint16 percentage) internal pure returns (uint96) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm hesitant to name the two functions the same. it might be easy to get confused by anyone using the library.
What do you think about this approach?
scaleBy
remains the common function that takes aprecision
scaleByBps
is the bps implementationscaleByPercentage
is the percentage implementation
It is more verbose, but I believe it is less error prone
test/BaseProverManager.t.sol
Outdated
function _calculatePercentageBPS(uint256 amount, uint16 percentage) internal pure returns (uint256) { | ||
return amount * percentage / 10_000; | ||
} | ||
|
||
function _calculatePercentage(uint256 amount, uint16 percentage) internal pure returns (uint256) { | ||
return amount * percentage / 100; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the tests just use the library? I don't think we are trying to check if the library works properly on these tests(we can create a new unit test for LibPercentage
if we wanted to do this)
It now uses scaleByBPS and scaleByPercentage explicitly
Changes to gas cost
🧾 Summary (10% most significant diffs)
Full diff report 👇
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great!
Merging this now that it has 2 approves. I had to change the foundry version that is used in the CI to stable because nightly was causing a couple tests that use |
This PR make two changes.
Firstly, move the percentage calculations to a separate library. This is cleaner in code and it also makes it easier to use different precision amounts.
The second is to use two decimals of precision for the delayed fee percentage. As a uint16 there are 65536 possible values but in basis points we can only represent numbers between 0 and 6.5 (albeit with very high precision). With two decimals we can represent numbers up to 655 (with less precision).